#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "employee.h"

#define FILENAME "employees.txt"

void inputEmployee(Employee* e) {
    printf("Enter Employee ID: ");
    scanf("%d", &e->id);
    printf("Enter Name: ");
    scanf("%s", e->name);
    printf("Enter Gender: ");
    scanf("%s", e->gender);
    printf("Enter Age: ");
    scanf("%d", &e->age);
    printf("Enter Education: ");
    scanf("%s", e->education);
    printf("Enter Salary: ");
    scanf("%lf", &e->salary);
    printf("Enter Address: ");
    scanf("%s", e->address);
    printf("Enter Phone: ");
    scanf("%s", e->phone);
}

void saveEmployeeToFile(Employee* e) {
    FILE* file = fopen(FILENAME, "a");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }
    fprintf(file, "%d %s %s %d %s %.2lf %s %s\n",
        e->id, e->name, e->gender, e->age, e->education, e->salary, e->address, e->phone);
    fclose(file);
    printf("Employee information saved successfully.\n");
}

void inputEmployeeInfo() {
    Employee e;
    inputEmployee(&e);
    saveEmployeeToFile(&e);
}

void browseEmployees() {
    FILE* file = fopen(FILENAME, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }
    Employee e;
    while (fscanf(file, "%d %49s %9s %d %49s %lf %99s %14s",
        &e.id, e.name, e.gender, &e.age, e.education, &e.salary, e.address, e.phone) == 8) {
        displayEmployee(&e);
    }
    fclose(file);
}

void displayEmployee(Employee* e) {
    printf("ID: %d\n", e->id);
    printf("Name: %s\n", e->name);
    printf("Gender: %s\n", e->gender);
    printf("Age: %d\n", e->age);
    printf("Education: %s\n", e->education);
    printf("Salary: %.2lf\n", e->salary);
    printf("Address: %s\n", e->address);
    printf("Phone: %s\n", e->phone);
    printf("\n");
}

void deleteEmployee(int id) {
    FILE* file = fopen(FILENAME, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }
    FILE* temp = fopen("temp.txt", "w");
    if (temp == NULL) {
        perror("Error opening temp file");
        fclose(file);
        return;
    }
    Employee e;
    int found = 0;
    while (fscanf(file, "%d %49s %9s %d %49s %lf %99s %14s",
        &e.id, e.name, e.gender, &e.age, e.education, &e.salary, e.address, e.phone) == 8) {
        if (e.id != id) {
            fprintf(temp, "%d %s %s %d %s %.2lf %s %s\n",
                e.id, e.name, e.gender, e.age, e.education, e.salary, e.address, e.phone);
        }
        else {
            found = 1;
        }
    }
    fclose(file);
    fclose(temp);
    remove(FILENAME);
    rename("temp.txt", FILENAME);
    if (found) {
        printf("Employee with ID %d deleted successfully.\n", id);
    }
    else {
        printf("No employee found with ID %d.\n", id);
    }
}

void modifyEmployee(int id) {
    FILE* file = fopen(FILENAME, "r+");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }
    Employee e;
    int found = 0;
    long pos;
    while ((pos = ftell(file)) != -1 && fscanf(file, "%d %49s %9s %d %49s %lf %99s %14s",
        &e.id, e.name, e.gender, &e.age, e.education, &e.salary, e.address, e.phone) == 8) {
        if (e.id == id) {
            printf("Enter new details for Employee ID %d:\n", id);
            inputEmployee(&e);
            fseek(file, pos, SEEK_SET);
            fprintf(file, "%d %s %s %d %s %.2lf %s %s\n",
                e.id, e.name, e.gender, e.age, e.education, e.salary, e.address, e.phone);
            found = 1;
            break;
        }
    }
    fclose(file);
    if (found) {
        printf("Employee with ID %d modified successfully.\n", id);
    }
    else {
        printf("No employee found with ID %d.\n", id);
    }
}
